package moodish.client;

import java.awt.Color;
import java.awt.Font;
import java.awt.Panel;
import java.awt.SystemColor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import moodish.comm.ClientComm;
import moodish.comm.ClientSideMessage;

public class Window_Main extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	private JTextField txtName;
	private JButton btnOptions = new JButton("Options");
	private JButton btnSignOut = new JButton("Sign Out");
	private JScrollPane scrollPaneFriends = new JScrollPane();
	private JScrollPane scrollPaneOtherPeople = new JScrollPane();

	private ClientComm clientComm;

	private Vector<Friend> friends = new Vector<Friend>();
	private Vector<OtherPeople> otherPeople = new Vector<OtherPeople>();

	private JList<Friend> friendsList = new JList<Friend>(friends);
	private JList<OtherPeople> otherPeopleList = new JList<OtherPeople>(
			otherPeople);

	/**
	 * Create the frame.
	 * 
	 * @param clientComm
	 * @param username
	 */
	public Window_Main(ClientComm clientComm, String username) {
		this.clientComm = clientComm;
		this.setLocation(600, 300);
		buildGui(username);

		displayMsg();

	}

	private void displayMsg() {
		Iterator<Friend> i;
		Iterator<OtherPeople> io;
		Friend f;
		OtherPeople other;
		while (clientComm.isConnected()) {
			i = friends.iterator();
			io = otherPeople.iterator();

			if (clientComm.hasNextMessage()) {
				ClientSideMessage next = clientComm.getNextMessage();

				switch (next.getType()) {
				case CONNECTED:
					other = new OtherPeople(next.getSendersNickname());
					otherPeople.add(other);
					break;

				case DISCONNECTED:
					while (i.hasNext()) {
						f = i.next();

						if (f.getName().equals(next.getPayload()))
							i.remove();

					}
					while (io.hasNext()) {
						if (io.next().getName().equals(next.getPayload()))
							io.remove();
					}
					break;
				case FRIENDSHIP:
					f = new Friend(next.getPayload(), "");
					friends.add(f);
					while (io.hasNext()) {
						if (io.next().getName().equals(next.getPayload()))
							io.remove();
					}
				case UNFRIENDSHIP:
					while (i.hasNext()) {
						f = i.next();

						if (f.getName().equals(next.getPayload()))
							i.remove();

					}
					otherPeople.add(new OtherPeople(next.getPayload()));
					break;
				case MOODISH_MESSAGE:
					for (int j = 0; j < friends.size() - 1; j++) {
						f = friends.get(j);
						if (f.getName().equals(next.getSendersNickname())) {
							f.setMood(next.getPayload());

							break;
						}

					}
					break;
				case ERROR:
					JOptionPane.showMessageDialog(null, "An error as occurred");
					break;

				}
				scrollPaneFriends.repaint();
				scrollPaneOtherPeople.repaint();

			}
		}

	}

	private class Listener implements ActionListener {
		private Window_Main frame;

		private void setWB(Window_Main frame) {
			this.frame = frame;
		}

		@Override
		public void actionPerformed(ActionEvent e) {

			if (e.getSource().equals(btnOptions)) {
				System.out.println("Options");
				Window_Option optionFrame = new Window_Option(clientComm);
				optionFrame.setVisible(true);

			}
			if (e.getSource().equals(btnSignOut)) {
				System.out.println("Signing Out");

				clientComm.disconnect();

				frame.dispose();
				System.exit(0);
			}
		}
	}

	private void buildGui(String username) {
		setResizable(false);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 799, 762);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		Listener listener = new Listener();
		listener.setWB(this);

		Panel MyArea = new Panel();
		MyArea.setBounds(15, 588, 768, 124);
		contentPane.add(MyArea);
		MyArea.setBackground(new Color(100, 149, 237));
		MyArea.setLayout(null);

		txtName = new JTextField();
		txtName.setFont(new Font("Comic Sans MS", Font.PLAIN, 20));
		txtName.setBackground(new Color(100, 149, 237));
		txtName.setForeground(SystemColor.window);
		txtName.setText(username);
		txtName.setEditable(false);
		txtName.setBounds(15, 24, 227, 26);
		MyArea.add(txtName);
		txtName.setColumns(10);

		JTextArea txtrMood = new JTextArea();
		txtrMood.setFont(new Font("Monospaced", Font.PLAIN, 16));
		txtrMood.setText("Mood");
		txtrMood.setBounds(15, 89, 122, 26);
		MyArea.add(txtrMood);
		txtrMood.setEditable(false);
		btnOptions.setBackground(new Color(230, 230, 250));

		btnOptions.setBounds(636, 42, 122, 29);
		MyArea.add(btnOptions);
		btnOptions.addActionListener(listener);

		JRadioButton rdbtnOnline = new JRadioButton("Online");
		rdbtnOnline.setForeground(Color.WHITE);
		rdbtnOnline.setBackground(new Color(100, 149, 237));
		rdbtnOnline.setBounds(148, 88, 94, 29);
		MyArea.add(rdbtnOnline);
		btnSignOut.setBackground(new Color(230, 230, 250));

		btnSignOut.setBounds(636, 88, 122, 29);
		MyArea.add(btnSignOut);

		JLabel lblMood = new JLabel("MooD");
		lblMood.setForeground(Color.WHITE);
		lblMood.setFont(new Font("Tekton Pro", Font.PLAIN, 20));
		lblMood.setBounds(15, 66, 69, 20);
		MyArea.add(lblMood);

		JPanel OthersPanel = new JPanel();
		OthersPanel.setBackground(Color.WHITE);
		OthersPanel.setBounds(15, 16, 768, 570);
		contentPane.add(OthersPanel);
		OthersPanel.setLayout(null);

		scrollPaneFriends.setBounds(0, 52, 380, 518);
		OthersPanel.add(scrollPaneFriends);

		scrollPaneOtherPeople.setBounds(382, 52, 386, 518);
		OthersPanel.add(scrollPaneOtherPeople);

		JLabel lblHowYourFriends = new JLabel("How your Friends are feeling:");
		lblHowYourFriends.setFont(new Font("Tahoma", Font.PLAIN, 19));
		lblHowYourFriends.setBounds(15, 16, 365, 20);
		OthersPanel.add(lblHowYourFriends);

		JLabel lblOtherPeopleOnline = new JLabel("Other People Online");
		lblOtherPeopleOnline.setFont(new Font("Tahoma", Font.PLAIN, 19));
		lblOtherPeopleOnline.setBounds(395, 16, 373, 20);
		OthersPanel.add(lblOtherPeopleOnline);
		btnSignOut.addActionListener(listener);

		scrollPaneOtherPeople.setViewportView(otherPeopleList);
		scrollPaneFriends.setViewportView(friendsList);
	}
}
